package com.jianbing.north.service.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.util.DateUtils;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.north.domain.NorthUserRecordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianbing.north.mapper.NorthAnswerRecordMapper;
import com.jianbing.north.domain.NorthAnswerRecord;
import com.jianbing.north.service.INorthAnswerRecordService;

/**
 * 用户测试结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class NorthAnswerRecordServiceImpl implements INorthAnswerRecordService 
{
    @Autowired
    private NorthAnswerRecordMapper northAnswerRecordMapper;

    private static final Map<Long,Long> partNum;

    static {
        partNum = new HashMap<>();
        partNum.put(1L,3L);
        partNum.put(2L,4L);
    }

    /**
     * 查询用户测试结果
     * 
     * @param id 用户测试结果主键
     * @return 用户测试结果
     */
    @Override
    public NorthAnswerRecord selectNorthAnswerRecordById(Long id)
    {
        return northAnswerRecordMapper.selectNorthAnswerRecordById(id);
    }

    /**
     * 查询用户测试结果列表
     * 
     * @param northAnswerRecord 用户测试结果
     * @return 用户测试结果
     */
    @Override
    public List<NorthAnswerRecord> selectNorthAnswerRecordList(NorthAnswerRecord northAnswerRecord)
    {
        return northAnswerRecordMapper.selectNorthAnswerRecordList(northAnswerRecord);
    }

    /**
     * 新增用户测试结果
     * 
     * @param northAnswerRecord 用户测试结果
     * @return 结果
     */
    @Override
    public int insertNorthAnswerRecord(NorthAnswerRecord northAnswerRecord)
    {
        return northAnswerRecordMapper.insertNorthAnswerRecord(northAnswerRecord);
    }

    /**
     * 修改用户测试结果
     * 
     * @param northAnswerRecord 用户测试结果
     * @return 结果
     */
    @Override
    public int updateNorthAnswerRecord(NorthAnswerRecord northAnswerRecord)
    {
        return northAnswerRecordMapper.updateNorthAnswerRecord(northAnswerRecord);
    }

    /**
     * 批量删除用户测试结果
     * 
     * @param ids 需要删除的用户测试结果主键
     * @return 结果
     */
    @Override
    public int deleteNorthAnswerRecordByIds(Long[] ids)
    {
        return northAnswerRecordMapper.deleteNorthAnswerRecordByIds(ids);
    }

    /**
     * 删除用户测试结果信息
     * 
     * @param id 用户测试结果主键
     * @return 结果
     */
    @Override
    public int deleteNorthAnswerRecordById(Long id)
    {
        return northAnswerRecordMapper.deleteNorthAnswerRecordById(id);
    }

    @Override
    public List<Timestamp> getEverydayRightNumByUserIdAndPartAndDifficulty(Long userId, Long part, Long difficulty) {
        return northAnswerRecordMapper.selectEverydayRightNumByUserIdAndPartAndDifficulty(userId, part,difficulty);
    }

    @Override
    public List<NorthUserRecordDTO> selectNorthAnswerRecordSummaryList(Long userId, Long part) {
        List<NorthUserRecordDTO> northUserRecordDTOS = northAnswerRecordMapper.selectNorthAnswerRecordSummaryList(userId, part);
        for(NorthUserRecordDTO northUserRecordDTO:northUserRecordDTOS){
            Long allNum = northUserRecordDTO.getAllNum();
            if (allNum != 0) {
                if(part==1){
                    Double accuracy = northUserRecordDTO.getFraction().doubleValue() / allNum.doubleValue();
                    accuracy = accuracy<0?0:accuracy;
                    northUserRecordDTO.setAccuracy(accuracy);
                }
                else {
                    Long rightNum = northUserRecordDTO.getRightNum();
                    northUserRecordDTO.setAccuracy(rightNum.doubleValue() / allNum.doubleValue());
                }
            }
            if(northUserRecordDTO.getTimes()<partNum.get(part)){
                northUserRecordDTO.setEndTime(null);
                northUserRecordDTO.setAccuracy(null);
            }else{
                Date begin = northUserRecordDTO.getBeginTime();
                Date end = northUserRecordDTO.getEndTime();
                Date date = null;
                String timeFormat = null;
                if(begin!=null && end!=null){
                    Duration duration = Duration.between(begin.toInstant(),end.toInstant());
                    Long time = duration.getSeconds();
                    timeFormat = String.format("%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60);
                    try {
                        date = new SimpleDateFormat("HH:mm:ss").parse(timeFormat);
                    }catch (ParseException exception){
                        date = null;
                    }
                }
                northUserRecordDTO.setDuration(date);
            }
        }
        return northUserRecordDTOS;
    }

    @Override
    public Long getFraction(Long userId,Integer part) {
        return northAnswerRecordMapper.getFraction(userId,part);
    }
}
